package com.excel.demo.excel.util;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.excel.demo.excel.model.ReadExcel;
import com.excel.demo.excel.service.ReadExcelService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

/**
 * @ClassName threadReadExcel
 * @Description TODO
 * @Author Administrator
 * @Date 2022/4/13 18:46
 * @Version 1.0
 */
@Component
public class ThreadReadExcel {
    private static final Logger LOGGER = LoggerFactory.getLogger(ThreadReadExcel.class);

    @Autowired
    private ReadExcelService readExcelService;

    @Async("uploadThreadPool")
    public Future<List<ReadExcel>> importExcel(MultipartFile file,  CountDownLatch latch) throws Exception {
        LOGGER.info("thread-start：【"+Thread.currentThread().getName()+"】");
        Workbook hssfWorkbook = getWorkBook(file);
        //获取sheet数量
        int sheetNum = hssfWorkbook.getNumberOfSheets();

        LOGGER.info("excel-sheetNum：" + sheetNum);
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(2);
//            params.setKeyIndex(null);//设置读取空值s

        List<ReadExcel> newList = new ArrayList<ReadExcel>();
        for (int sheetIndex = 0; sheetIndex < sheetNum; sheetIndex++) {
            params.setStartSheetIndex(sheetIndex);//开始的sheet下标
            List<ReadExcel> list = ExcelImportUtil.importExcel(
                    file.getInputStream(),
                    ReadExcel.class, params
            );
            for(ReadExcel re:list){
                re.setSheetindex(sheetIndex);
                re.setSheetname(hssfWorkbook.getSheetName(sheetIndex));
                newList.add(re);
            }
        }
        Integer  count = readExcelService.saveReadExcelBatch(newList);
        LOGGER.info("excel-save:" + count);
        LOGGER.info("thread-end：【"+Thread.currentThread().getName()+"】");
        latch.countDown();
        return new AsyncResult<>(newList);
    }

    public static Workbook getWorkBook(MultipartFile file) throws IOException {
        //这样写excel能兼容03和07
        InputStream is = file.getInputStream();
        Workbook hssfWorkbook = null;
        try {
            hssfWorkbook = new HSSFWorkbook(is);
        } catch (Exception ex) {
            is = file.getInputStream();
            hssfWorkbook = new XSSFWorkbook(is);
        }
        return hssfWorkbook;
    }
}
